package com.cy.pj.service.impl;

import com.cy.pj.Dao.SysDeptDao;
import com.cy.pj.common.exception业务异常.ServiceException;
import com.cy.pj.common.pojo.Node;
import com.cy.pj.common.pojo.SysDept;
import com.cy.pj.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = false,
        rollbackFor = Throwable.class,
        isolation = Isolation.READ_COMMITTED)
@Service
public class SysDeptServiceImpl implements SysDeptService {
    @Autowired
    private SysDeptDao sysDeptDao;

    @Transactional(readOnly = true)
    @Override
    public List<SysDept> findDeptsService() {
        List<SysDept> list = sysDeptDao.selectDepts();
        if (list == null || list.size() == 0)
            throw new ServiceException("没有部门信息");
        return list;
    }

    @Transactional(readOnly = true)
    @Override
    public List<Node> findDeptTreeNodesService() {
        List<Node> list =
                sysDeptDao.selectDeptTreeNodes();
        if (list == null || list.size() == 0)
            throw new ServiceException("没有部门信息");
        return list;
    }

    @Transactional(readOnly = true)
    @Override
    public SysDept findByIdService(Integer id) {
        SysDept sysDept = sysDeptDao.selectById(id);
        if (sysDept == null)
            throw new ServiceException("没有部门信息");
        return sysDept;
    }

    @Transactional
    @Override
    public int updateDeptsService(SysDept sysDept) {
        if (sysDept == null)
            throw new ServiceException("更新内容不能为空");
        int rows = sysDeptDao.updateDepts(sysDept);
        if (rows == 0)
            throw new ServiceException("数据不存在");
        return rows;
    }

    @Transactional
    @Override
    public int insertDeptsService(SysDept sysDept) {
        int rows = sysDeptDao.insertDepts(sysDept);
    return rows;
    }
}